package com.dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

import com.bean.AdminBean;
import com.db.JdbcUtil;
import com.exception.AdminException;
import com.exception.DBException;

public class AdminDao
{

	private Connection con = null;
	private PreparedStatement ps = null;
	private ResultSet rs = null;

	/**
	 * add a new Administrator
	 * 
	 * @param admin
	 *            bean
	 * @return mark
	 * @throws DBException
	 * @throws AdminException
	 */
	public void addAdministrator(AdminBean admin) throws DBException,
			AdminException
	{
		String sql = "insert into lab_administrator(adminID,firstname,lastname,password,email,remark)"
				+ " values(?,?,?,?,?,?)";
		con = JdbcUtil.getConnection();
		try
		{
			ps = con.prepareStatement(sql);
			ps.setString(1, admin.getAdminID());
			ps.setString(2, admin.getFirstname());
			ps.setString(3, admin.getLastname());
			ps.setString(4, admin.getPassword());
			ps.setString(5, admin.getEmail());
			ps.setString(6, admin.getRemark());
			ps.executeUpdate();
		}
		catch (Exception e)
		{
			throw new AdminException("Fail to add a administrator");
		}
		finally
		{
			JdbcUtil.free(rs, ps, con);
		}
	}

	/**
	 * update administrator as adminID
	 * 
	 * @param admin
	 * @return mark whether is it execute successfully
	 * @throws DBException
	 * @throws AdminException
	 */
	public void updateAdministrator(AdminBean admin) throws DBException,
			AdminException
	{
		String sql = "update lab_administrator set firstname=?,lastname=?,password=?,email=?,remark=? where adminID=?";

		con = JdbcUtil.getConnection();
		try
		{
			ps = con.prepareStatement(sql);
			ps.setString(1, admin.getFirstname());
			ps.setString(2, admin.getLastname());
			ps.setString(3, admin.getPassword());
			ps.setString(4, admin.getEmail());
			ps.setString(5, admin.getRemark());
			ps.setString(6, admin.getAdminID());
			ps.executeUpdate();
		}
		catch (SQLException e)
		{
			throw new AdminException("Fail to update administrator info");
		}
		finally
		{
			JdbcUtil.free(rs, ps, con);
		}
	}

	/**
	 * delete administrator as adminID
	 * 
	 * @param admin
	 * @return mark whether is it execute successfully
	 * @throws AdminException
	 * @throws DBException
	 */
	public void deleteAdministrator(AdminBean admin) throws AdminException,
			DBException
	{
		String sql = "delete from administrator where adminID=?";
		con = JdbcUtil.getConnection();

		try
		{
			ps = con.prepareStatement(sql);
			ps.setString(1, admin.getAdminID());
			ps.executeUpdate();
		}
		catch (SQLException e)
		{
			throw new AdminException("Fail to delete administrator");
		}
		finally
		{
			JdbcUtil.free(rs, ps, con);
		}
	}
	/**
	 * confirm the administrator,login the system if is the administrator
	 * information store in the database
	 * 
	 * @param admin
	 * @return boolean
	 * @throws AdminException
	 * @throws DBException
	 */
	public boolean administratorLogin(AdminBean admin) throws AdminException,
			DBException
	{
		String sql = "select adminID,password,firstname "
				+ "from lab_administrator" + " where adminID='"
				+ admin.getAdminID() + "' and password='" + admin.getPassword()
				+ "'";
		boolean flag = false;
		con = JdbcUtil.getConnection();
		try
		{
			ps = con.prepareStatement(sql);
			rs = ps.executeQuery();
			while (rs.next())
			{
				admin.setFirstname(rs.getString("firstname"));
				flag = true; // if the administrator exist, return true
			}
		}
		catch (Exception e)
		{
			throw new AdminException("Fail to login");
		}
		finally
		{
			JdbcUtil.free(rs, ps, con);
		}
		return flag;
	}

	/**
	 * query the administrator infor as ID
	 * 
	 * @param adminBean
	 * @return list
	 * @throws DBException
	 * @throws AdminException
	 */
	public void getAdministratorAsID(AdminBean admin) throws DBException,
			AdminException
	{
		String sql = "select * from lab_administrator " + "where adminID = '"
				+ admin.getAdminID() + "'";
		con = JdbcUtil.getConnection();
		try
		{
			ps = con.prepareStatement(sql);
			rs = ps.executeQuery();
			while (rs.next())
			{
				admin.setAdminID(rs.getString("adminID"));
				admin.setFirstname(rs.getString("firstname"));
				admin.setLastname(rs.getString("lastname"));
				admin.setPassword(rs.getString("password"));
				admin.setEmail(rs.getString("email"));
				admin.setRemark(rs.getString("remark"));
			}
		}
		catch (Exception e)
		{
			throw new AdminException("Fail to get admin info");
		}
		finally
		{
			JdbcUtil.free(rs, ps, con);
		}
	}

}
